package com.liruo.treasureattic.authserver.model;

import com.liruo.treasureattic.commmon.constant.AuthServerConstant;
import com.liruo.treasureattic.common.model.dto.AuthUser;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.Arrays;
import java.util.Collection;
import java.util.stream.Collectors;

/**
 * @Author:liruo
 * @Date:2022-10-09-23:22:41
 * @Desc
 */
@Data
public class SecurityUser implements UserDetails {
    private Long id;
    private String username;
    private String pwd;
    private Boolean deleted;
    private  Collection<? extends GrantedAuthority> authorities;

    public SecurityUser(AuthUser user) {
        this.id = user.getId();
        this.username = user.getUsername();
        this.pwd = user.getPwd();
        this.deleted = user.getDeleted();
        this.authorities = Arrays.stream(user.getAuthority()
                .split(AuthServerConstant.AUTHORITY_SEPARATOR))
                .map(SimpleGrantedAuthority::new)
                .collect(Collectors.toSet());
    }

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        return this.authorities;
    }

    @Override
    public String getPassword() {
        return this.pwd;
    }

    @Override
    public String getUsername() {
        return this.username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return !this.deleted;
    }
}
